package com.dudu.access;

import java.util.Calendar;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.UUID;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.service.ILogService;
import com.dudu.service.impl.LogService;
import com.dudu.utils.DateUtils;

/**
 * 
 * 百悟 通道短信Util
 * 
 */

public class BaiWuSMS {

    private static final Logger log = core.util.Logger.getLogger(BaiWuSMS.class.getName());

    private static ILogService logService = new LogService();

    public static void main(String[] args) throws Exception {
	String mobiles = "17001090122";
	String content = "【jack】jintainbadianjiukeyidaojiale";
	String msg_id = UUID.randomUUID().toString();
	String ext_number = "666";
	sendSms(mobiles, content, msg_id, ext_number);
    }

    public static String sendSms(String mobiles, String content, String msg_id, String ext_number) {
	String result = "";
	StringBuffer requestBody = new StringBuffer();
	long begin = Calendar.getInstance().getTimeInMillis();
	HttpClient client = new HttpClient();
	PostMethod post = null;
	try {
	    ResourceBundle resourceBundle = PropertyResourceBundle.getBundle("smsconfig");
	    String service_url = resourceBundle.getString("service_url");
	    requestBody.append("service_url=" + service_url);
	    String corp_id = resourceBundle.getString("corp_id");
	    requestBody.append(";corp_id=" + corp_id);
	    String corp_pwd = resourceBundle.getString("corp_pwd");
	    requestBody.append(";corp_pwd=" + corp_pwd);
	    String corp_service = resourceBundle.getString("corp_service");
	    requestBody.append(";corp_service=" + corp_service);
	    String MD5_td_code = resourceBundle.getString("MD5_td_code");
	    requestBody.append(";MD5_td_code=" + MD5_td_code);
	    requestBody.append(";msg_id=" + msg_id);
	    requestBody.append(";mobile=" + mobiles);
	    requestBody.append(";msg_content=" + content);
	    requestBody.append(";ext=" + ext_number);
	    post = new PostMethod(service_url);
	    post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
	    NameValuePair[] data = {
		    // 百悟1.0接口参数
		    new NameValuePair("id", corp_id), new NameValuePair("MD5_td_code", MD5_td_code), new NameValuePair("msg_id", msg_id),
		    // 百悟2.0接口参数
		    new NameValuePair("corp_id", corp_id), new NameValuePair("corp_pwd", corp_pwd), new NameValuePair("corp_service", corp_service),
		    new NameValuePair("corp_msg_id", msg_id),
		    // 1.0、2.0公共参数
		    new NameValuePair("mobile", mobiles), // 多个号码用半角英文逗号隔开,一次最多不能超过200个
		    new NameValuePair("msg_content", content), new NameValuePair("ext", ext_number) };

	    post.setRequestBody(data);

	    client.executeMethod(post);
	    /*
	     * Header[] headers = (Header[]) post.getResponseHeaders(); int
	     * statusCode = post.getStatusCode();
	     * log.info("-----sendSms response statusCode:"+statusCode); for
	     * (Header h : headers) {
	     * log.info("-----sendSms response header:"+h.toString()); }
	     */
	    result = new String(post.getResponseBodyAsString());

	    log.info("-----sendSms response:" + result);
	    // logService //写入系统日志
	} catch (Exception ex) {
	    ex.printStackTrace();
	    // 记录日志
	    log.info("-----sendSms exception:" + ex.getMessage());
	} finally {
	    try {
		post.releaseConnection();
		/**
		 * httpclient的releaseConnection没有真正关闭连接 会导致存在close_wait的http连接
		 */
		((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
	    } catch (Exception ex) {
		ex.printStackTrace();
	    }
	}
	long end = Calendar.getInstance().getTimeInMillis();
	String result_status = "-1";
	if (StringUtils.isNotBlank(result) && result.startsWith("0")) {
	    result_status = "0";
	}

	logService.logIms(Double.valueOf((end - begin)) / 1000, "SMMS", requestBody.toString(), result, result_status, DateUtils.getCurrentTime(), "BaiWu", mobiles);
	return result;
    }

}
